package sagroup.testing;

/**  
 -- The Performance Driven Lime Project -- 

 A self adaptive tuple space for mobile agents and hosts by dynamic
 adapting tuple's replication policy.

 Copyright (C) 2008
 Xing Jiankuan, Pan Wenhui
 SAGroup of Tsinghua University

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received this copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

import lights.adapters.Field;
import lights.adapters.Tuple;
import lights.interfaces.ITuple;

import lime2.Location;
import lime2.PolicyException;
import lime2.TupleID;

import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.FlatShareTuple;
import sagroup.dracots.TupleType;
import sagroup.dracots.testing.ListTuple;

/**
 * @author Xing Jiankuan
 * 
 */
public class TestFireWeakReaction implements Runnable {
	DracoTS dts = null;
	private FlatShareTuple makeNewTuple(String typeName, int num) {
		ITuple it = new Tuple();
		it.add(new Field().setValue(num));
		return new FlatShareTuple(new TupleType(typeName), it);
	}
	public void run() {
		try {
			dts = DracoTS.get("Test");
			HashMap<String, LinkedList<FlatShareTuple>> tuples = new HashMap<String, LinkedList<FlatShareTuple>>();
			for (int i = 'A'; i <= 'B'; i++) {
				String type = String.valueOf((char) i);
				LinkedList<FlatShareTuple> list = new LinkedList<FlatShareTuple>();
				tuples.put(type, list);
				for (int j = 1; j <= 3; j++) {
					list.add(makeNewTuple(type, j));
				}
			}
			for (LinkedList<FlatShareTuple> list : tuples.values()) {
				for (FlatShareTuple t : list) {
					dts.out(t);
					System.out.println("Agent out tuple " + t.getType() + "-"
							+ t.getVID());
				}
			}
			FlatShareTuple template = (FlatShareTuple) FlatShareTuple
					.createMatchAllTemplateByClass(FlatShareTuple.class);
			template.setCurr(Location.UNSPECIFIED);
			template.setDest(Location.UNSPECIFIED);
			template.setType(new TupleType("A"));
			DracoTuple ret = dts.rd(template);
			System.out.println("rd tuple :" + ret.getType());
		} catch (Exception ex) {
			System.out.print(ex.getMessage());
		}
		System.out.println("Test Agent A is done");
	}
}
